package com.shushan.pages;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.net.bean.NetRetBean;
import com.android.net.core.common.RequestType;
import com.android.net.helper.NetHelper;
import com.android.net.listener.common.CallbackCode;
import com.android.net.url.UrlParse;
import com.shushan.base.BaseActivity;
import com.shushan.base.Constants;
import com.shushan.base.ScreenManager;
import com.shushan.db.ChildBeanDao;
import com.shushan.home.adapter.StarMallRecyclerAdapter;
import com.shushan.home.bean.StarsChildBean;
import com.shushan.home.bean.StarsDataBean;
import com.shushan.login.bean.ChildBean;
import com.shushan.login.bean.UserBean;
import com.shushan.network.UrlCommParse;
import com.shushan.network.UrlConst;
import com.shushan.network.listener.NetSingleCommListener;
import com.shushan.pages.bean.MaketingDataBean;
import com.shushan.pages.bean.MyStarsDataBean;
import com.shushan.shushanbao.BabyApplication;
import com.shushan.shushanbao.R;
import com.shushan.ui.GridRecyclerDivider;
import com.shushan.util.DensityUtil;
import com.shushan.util.ImageUtils;
import com.shushan.util.Session;
import com.shushan.util.SharedPreferencesUtils;
import com.shushan.util.ToolUtils;
import com.shushan.web.BaseWebActivity;
import com.shushan.web.GoodsDetailWebActivity;

import java.util.List;

/**
 * @ClassName: MyStarActivity
 * @Desciption: //我的学贝页面
 * @author: zhangshihao
 * @date: 2018-05-10
 */
public class MyStarActivity extends BaseActivity implements View.OnClickListener {

    private static final String TAG = MyStarActivity.class.getSimpleName();

    private TextView tvStarNum;
    private RelativeLayout rlStarUsage;
    private RelativeLayout rlExchangeRecord;
    private RelativeLayout rlHowToGetStar;
    private RelativeLayout rlExchangeReward;

    private BabyApplication shuShanApplication = BabyApplication.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_star);
        ScreenManager.getInstance().setStatusBar(true,this);

        initView();
    }

    private void initView(){
        tvStarNum = findViewById(R.id.tv_star_num);
        tvStarNum.setText(shuShanApplication.getCurrentChildBean().stars+"颗");
        rlStarUsage = findViewById(R.id.rl_star_usage);
        rlStarUsage.setOnClickListener(this);
        rlExchangeRecord = findViewById(R.id.rl_exchange_record);
        rlExchangeRecord.setOnClickListener(this);
        rlHowToGetStar = findViewById(R.id.rl_how_to_get_star);
        rlHowToGetStar.setOnClickListener(this);
        rlExchangeReward = findViewById(R.id.rl_exchange_reward);
        rlExchangeReward.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.rl_star_usage:
                gotoUseWeb();
                break;
            case R.id.rl_how_to_get_star:
                gotoGetWeb();
                break;
            case R.id.rl_exchange_record:
                gotoExchangeRecordWeb();
                break;
            case R.id.rl_exchange_reward:
                startActivityForResultAllow(new Intent(this,StarMallActivity.class),400);
                break;
        }
    }

    private void gotoExchangeRecordWeb(){
        int childId = shuShanApplication.getCurrentChildId();
        UrlParse urlParse = new UrlParse(/*UrlConst.BASE_URL*/"https://tpi.shushan.com")
                .appendRegion(UrlConst.XUEBEI_EXCHANGE_RECORD)
                .putValue("childid", (childId > 0) ? (childId+"") : "");

        Intent intent = new Intent(this, BaseWebActivity.class);
        intent.putExtra("title","学贝兑换记录");
        intent.putExtra("url",urlParse.toString());
        startActivityAllow(intent);
    }

    private void gotoGetWeb() {
        int childId = shuShanApplication.getCurrentChildId();
        UrlParse urlParse = new UrlParse(/*UrlConst.BASE_URL*/"https://tpi.shushan.com")
                .appendRegion(UrlConst.XUEBEI_INTRO_HTML)
                .putValue("childid", (childId > 0) ? (childId+"") : "");

        Intent intent = new Intent(this, BaseWebActivity.class);
        intent.putExtra("title","如何获得学贝");
        intent.putExtra("url",urlParse.toString());
        startActivityAllow(intent);
    }

    private void gotoUseWeb() {
        int childId = shuShanApplication.getCurrentChildId();
        UrlParse urlParse = new UrlParse(/*UrlConst.BASE_URL*/"https://tpi.shushan.com")
                .appendRegion(UrlConst.XUEBEI_USE_HTML)
                .putValue("childid", (childId > 0) ? (childId+"") : "");

        Intent intent = new Intent(this, BaseWebActivity.class);
        intent.putExtra("title","学贝的用途");
        intent.putExtra("url",urlParse.toString());
        startActivityAllow(intent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 400){
            getUserinfo();
        }
        if(resultCode == 300){
            onBackAction();
        }else if(resultCode == 500){//玩具详情点击购买
            startActivity(OpenVipSpendActivity.class);
        }
    }

    public void getUserinfo() {
        UrlParse urlParse = new UrlCommParse(this, UrlConst.BASE_URL).appendRegion(UrlConst.GET_USER_INFO);
        urlParse.putValue("phone",SharedPreferencesUtils.getStringDate(Constants.LOGIN_MOBILE_SHAREPREFERS_KEY));

        NetHelper.create()
                .url(urlParse.toStringOnlyHeader())
                .param(urlParse.toStringOnlyParam())
                .request(RequestType.REQUEST_TYPE_POST, new NetSingleCommListener<UserBean>() {

                    @Override
                    protected void onSuccess(UserBean userBean) {
                        if(userBean != null){
                            shuShanApplication.setUserBean(userBean);

                            if(!TextUtils.isEmpty(userBean.password)){
                                SharedPreferencesUtils.setStringDate(Constants.PARENT_PASSWD_SHAREPREFERS_KEY,userBean.password);
                            }

                            List<ChildBean> childList = userBean.getChildList();
                            if(childList.size() > 0){
                                new ChildBeanDao(shuShanApplication).dropAndAddList(childList);
                            }else {
                                new ChildBeanDao(shuShanApplication.getInstance()).clearTab();
                            }

                            ChildBean currentChildBean = shuShanApplication.getCurrentChildBean();
                            if(currentChildBean != null){
                                tvStarNum.setText(currentChildBean.stars+"");
                            }
                        }
                    }

                    @Override
                    protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
                        showNoMsgError(netRetBean);
                    }
                });
    }
}
